Dictionaries
The dictionary data type in Python is used to store key-value pairs. Essentially, we are mapping a key to the value. This is why dictionaries are sometimes called maps or hashmaps in other programming languages.
For example, we can store a dictionary with three key-value pairs. The keys represent the names and the values represent their age:

1. Creating Dictionaries
A dictionary is created using curly braces {} similar to a set. But dictionaries don't just store values, they store key-value pairs separated by commas.
- The key and value are separated by a colon :, with the key on the left and the value on the right.
my_dict = {
"Alice": 25,
"Bob": 30,
"Charlie": 35
}
To declare an empty dictionary we can use empty curly braces {}. We can then add key-value pairs to the dictionary using square brackets [] and the assignment operator =. Keys don’t have to be integers.
my_dict = {}
# adding key-value pairs to the dictionary
my_dict["Alice"] = 25
my_dict["Bob"] = 30
my_dict["Charlie"] = 35
2. Dictionary Operations
Dictionaries can't contain duplicate keys. If we assign the same key a new value, the old value is overwritten.
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict["a"]) # Output: 1
my_dict["a"] = 4
print(my_dict["a"]) # Output: 4
The values within a dictionary can be of any type, including lists, sets, and even other dictionaries.
my_dict = {
"a": [1, 2, 3],
"b": {4, 5, 6},
"c": {
"x": 7,
"y": 8,
"z": 9
}
}
print(my_dict["a"]) # Output: [1, 2, 3]
print(my_dict["b"]) # Output: {4, 5, 6}
print(my_dict["c"]) # Output: {"x": 7, "y": 8, "z": 9}
To check if a dictionary contains a key, you can use the in keyword.
my_dict = {"a": 1, "b": 2, "c": 3}
print("a" in my_dict) # Output: True
print("d" in my_dict) # Output: False
3. Remove Item from Dictionary
The pop() method takes a key as an argument and removes the key-value pair from the dictionary. If the key doesn’t exist, it will raise a KeyError.
my_dict = {"a": 1, "b": 2, "c": 3}
my_dict.pop("a")
print(my_dict) # Output: {"b": 2, "c": 3}
my_dict.pop("d") # Raises KeyError
If you don't want to worry about handling the KeyError, you can use the second argument of the pop() function. This argument is the default value that will be returned if the key doesn't exist.
my_dict = {"a": 1, "b": 2, "c": 3}
value = my_dict.pop("d", 0) # Returns 0, no error occurs
You can also use the del keyword to remove a key-value pair from a dictionary. This is a bit more concise than using the pop() function.
my_dict = {"a": 1, "b": 2, "c": 3}
del my_dict["a"]
4. Dictionary Looping
Using len() to get the length of a dictionary will return the number of key-value pairs in the dictionary.
my_dict = {"a": 1, "b": 2, "c": 3}
print(len(my_dict)) # Output: 3
But just like with sets, the length of a dict won't help us to loop over it. By using the for loop, we can iterate over the keys in the dictionary by using the in operator. We can then use the key to access the value associated with that key.
my_dict = {"a": 1, "b": 2, "c": 3}
for key in my_dict:
value = my_dict[key]
print(key, value)
1. items() method
We can also use the items() method to loop over both the keys and values at the same time. With items(), we’ll need to have two variables to unpack the key-value pair. The first will be the key, and the second will be the value.
my_dict = {"a": 1, "b": 2, "c": 3}
for key, value in my_dict.items():
print(key, value)
2. values() method
Another way of iterating over a dictionary is by using the values() method. This allows us to loop over the values in the dictionary without needing to access the keys.
my_dict = {"a": 1, "b": 2, "c": 3}
for value in my_dict.values():
print(value)
A useful use case for this is when we want to convert the values of a dictionary into a list. This can be done by using the list() function.
my_dict = {"a": 1, "b": 2, "c": 3}
values = list(my_dict.values())
print(values) # Output: [1, 2, 3]